package com.wwu.client;

import com.wwu.common.WsConst;
import org.apache.cxf.binding.soap.SoapMessage;
import org.apache.cxf.headers.Header;
import org.apache.cxf.helpers.DOMUtils;
import org.apache.cxf.interceptor.Fault;
import org.apache.cxf.phase.AbstractPhaseInterceptor;
import org.apache.cxf.phase.Phase;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import javax.xml.namespace.QName;
import java.util.List;


/**
 * 客户端登陆拦截器
 * @Author: 一蓑烟雨
 * @CreateTime: 2021-10-30 21:13
 * @Version: 1.0.0
 **/
public class ClientLoginInterceptor extends AbstractPhaseInterceptor<SoapMessage> {
    /**
     * 用户名
     */
    private  String username;
    /**
     * 密码
     */
    private String password;

    public ClientLoginInterceptor(String username,String password) {
        super(Phase.PREPARE_SEND);
        this.username = username;
        this.password = password;
    }

    /**
     *  客户端设置请求头部信息
     * @return: void
     * @Author: 一蓑烟雨
     * @Date: 2021/10/30 0030 下午 21:22
     */
    @Override
    public void handleMessage(SoapMessage soapMessage) throws Fault {
        //获取全部头信息
        List<Header> headers = soapMessage.getHeaders();
        //创建Document文档
        Document document = DOMUtils.createDocument();
        //创建认证数据信息
        Element auth = document.createElementNS(WsConst.NAMESPACE_URI,"authority");
        Element username = document.createElement("username");
        Element password = document.createElement("password");
        username.setTextContent(this.username);
        password.setTextContent(this.password);
        auth.appendChild(username);
        auth.appendChild(password);
        headers.add(0,new Header(new QName("authority"),auth));
    }
}
